<?php
namespace app\admin\model;
use think\Db;
use think\Model;
class DiscountModel extends Model{
    public function index(){
        return Db::name('discount')->order('id desc')->paginate(10);
    }
    public function addVolume(){
        return Db::name('car_wash')->order('wa_id desc')->select();
    }
    public function userList(){
        return Db::name('wx_user')->order('id desc')->paginate(10);
    }
    public function userSearch($mobile){
        return Db::name('wx_user')->where('mobile',$mobile)->paginate(10);
    }
    public function volumeRepair($id){
        return Db::name('discount')->where('id',$id)->find();
    }
    public function appointdels($id){
        return Db::name('wx_user_discount')->where('id',$id)->delete();
    }
    public function del($id){
        Db::startTrans();
        try {
            Db::name('discount')->where('id',$id)->delete();
            Db::name('wx_user_discount')->where('volume_id',$id)->delete();
            Db::commit();//提交事务
            return true;
        } catch (\Exception $e) {
            Db::rollback();//事务回滚
            return false;
        }
    }
    public function userAdd($volume_id,$id){
        $data['user_id']    = $id;
        $data['volume_id']  = $volume_id;
        $data['time']    = time();
        Db::name('wx_user_discount')->insert($data);
        return true;
    }
    public function dataVerification($value){
        if (empty($value)){
            return "请添加参数";
        }
        $date = $this->add($value);
        if (is_string($date)){
            return $date;
        }
        Db::name('discount')->insert($date);
        return true;
    }
    public function dataVerifications($value){
        if (empty($value)){
            return "请添加参数";
        }
        $date = $this->add($value);
        if (is_string($date)){
            return $date;
        }
        Db::name('discount')->where('id',$value['id'])->update($date);
        return true;
    }

    public function userCard(){
        $discountid= Db::name('wx_user_discount')->column('user_id');
        $where['id'] =  array_unique($discountid);
        $userdata= Db::name('wx_user')->where($where)->paginate(10);
        return $userdata;
    }
    public function userCards($id){
        $volume_id['id']= Db::name('wx_user_discount')->where('user_id',$id)->column('volume_id');
        $data= Db::name('discount')->where($volume_id)->paginate(10);
        return $data;
    }
    public function add($value){
       if(empty($value['wa_standard']) || empty($value['wa_model']) || empty($value['living_time']) || empty($value['die_time']) || empty($value['name']) || empty($value['full'])){
           return '请添加参数';
        }
       if ($value['living_time'] > $value['die_time']){
           return "时间错误";
       }
        if ($value['full'] < $value['price']){
            return "最低消费大于满减金额";
        }
       if (!is_numeric($value['price'])){
           return "金额错误";
       }
       $just['standard']        = $value['wa_standard'];
       $just['model']           = $value['wa_model'];
       $just['add_time']        = time();
       $just['living_time']     = strtotime($value['living_time']);
       $just['die_time']        = strtotime($value['die_time']);
       $just['price']           = $value['price'];
       $just['name']           = $value['name'];
       $just['full']           = $value['full'];
       $just['state']           = $value['state'];
       return $just;
    }
}